//
//  ViewController.swift
//  石头剪子布
//
//  Created by adiljan on 16/8/30.
//  Copyright © 2016年 adiljan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    
    
    var rps = Rps()
    @IBOutlet weak var lableUp: UITextField!
    
    @IBOutlet weak var lableDown: UITextField!
    @IBOutlet weak var btnRock: UIButton!
    @IBOutlet weak var btnPaper: UIButton!
    @IBOutlet weak var btnScissor: UIButton!
    @IBOutlet weak var btnAgain: UIButton!
    
    // 设置圆角
    func a(){
        
        btnRock.layer.masksToBounds = true
        btnRock.layer.cornerRadius = 5
        btnPaper.layer.masksToBounds = true
        btnPaper.layer.cornerRadius = 5
        btnScissor.layer.masksToBounds = true
        btnScissor.layer.cornerRadius = 5
        btnAgain.layer.masksToBounds = true
        btnAgain.layer.cornerRadius = 5
    }
    
    @IBAction func Rock(sender: AnyObject) {
        rps.setStats(.Rock)
        judgement()
    }
    @IBAction func Paper(sender: AnyObject) {
        rps.setStats(.Paper)
        judgement()
    }
    @IBAction func Scissor(sender: AnyObject) {
        rps.setStats(.Scissor)
        judgement()
    }
    
    @IBAction func again(sender: AnyObject) {
        resetAll()
    }
    
    // 全局初始化
    func resetAll() {
        lableUp.text = ""
        lableDown.text = ""
        //        rps.setStats(nil)
    }
    // 判断输赢
    func judgement (){
        let randomRps = rps.randomRps()
        let currentStat = rps.getStats()
        let result = rps.compare(currentStat, anotherRpsStat: randomRps)
        screen(result, stat: currentStat, anotherStat: randomRps)
        
    }
    
    // 控制显示的方法
    func screen(result:Int,stat:Rps.Stats,anotherStat:Rps.Stats){
        var resultString = ""
        var statString = ""
        var anotherString = ""
        
        switch result {
        case 0:
            resultString = "输了"
        case 1:
            resultString = "赢了"
        case 2:
            resultString = "平局了"
        default:
            resultString = "醉"
        }
        
        switch stat {
        case .Rock:
            statString = "👊"
        case .Scissor:
            statString = "✌️"
        case .Paper:
            statString = "✋"
            
        }
        switch anotherStat {
        case .Rock:
            anotherString = "👊"
        case .Scissor:
            anotherString = "✌️"
        case .Paper:
            anotherString = "✋"
        }
        
        lableUp.text = "你" + resultString + "!"
        lableDown.text = "你出了" + statString + "，但电脑出了" + anotherString + "!"
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        a()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

